cmake_minimum_required(VERSION 3.23)

project(expertin VERSION 6.0)

set(CMAKE_MESSAGE_LOG_LEVEL VERBOSE)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# 添加 GmSSL 子目录
add_subdirectory(thirdparty/GmSSL)

set(PROJECT_SOURCES
        app.rc
        qml.qrc
        src/main.cpp
        src/singleton.h
        src/global/SysConstants.cpp src/global/SysConstants.h
        src/helper/CryptHelper.h src/helper/CryptHelper.cpp
        src/helper/SettingsHelper.h src/helper/SettingsHelper.cpp
        src/helper/Snowflake.h src/helper/Snowflake.cpp
        src/helper/FileHelper.h src/helper/FileHelper.cpp
        src/helper/ConnectionPool.h src/helper/ConnectionPool.cpp
        src/helper/SessionGuard.h
        src/helper/DatabaseManager.h src/helper/DatabaseManager.cpp
        src/helper/ConfigHelper.cpp src/helper/ConfigHelper.h
        src/dao/UserDao.h src/dao/UserDao.cpp
        src/service/UserService.h src/service/UserService.cpp
        src/entity/SysUser.h
        src/helper/AuthManager.h src/helper/AuthManager.cpp
        src/entity/SysOrg.h
        src/dao/OrgDao.h src/dao/OrgDao.cpp        
        src/service/OrgService.h src/service/OrgService.cpp
)

Set(RESOURCES_FILES

)

#导入Qt相关依赖包
find_package(QT NAMES Qt5 REQUIRED COMPONENTS Quick Network Sql Svg PrintSupport Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Quick Network Sql Svg PrintSupport Widgets)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(${PROJECT_NAME}
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET expertin6 APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(${PROJECT_NAME} SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(${PROJECT_NAME}
          ${PROJECT_SOURCES}
          config.ini
          tree_data.json
        )
    endif()
endif()

target_include_directories(${PROJECT_NAME} PRIVATE
  ${CMAKE_CURRENT_SOURCE_DIR}/src
)

target_link_libraries(${PROJECT_NAME} PRIVATE
  Qt${QT_VERSION_MAJOR}::Core
  Qt${QT_VERSION_MAJOR}::Network
  Qt${QT_VERSION_MAJOR}::Sql
  Qt${QT_VERSION_MAJOR}::PrintSupport
  Qt${QT_VERSION_MAJOR}::Quick
  gmssl  # GmSSL 目标名称
)

# 包含 GmSSL 头文件
target_include_directories(${PROJECT_NAME} PRIVATE
    thirdparty/GmSSL/include
)

# 可选：设置 RPATH
set_target_properties(${PROJECT_NAME} PROPERTIES
    BUILD_RPATH "$ORIGIN"
    INSTALL_RPATH "$ORIGIN"
)

if(UNIX)
    find_package(Threads REQUIRED)
    target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads)
elseif(WIN32)
    target_link_libraries(${PROJECT_NAME} PRIVATE $<$<PLATFORM_ID:Windows>:ws2_32 crypt32>)
endif()

# 定义需要复制的文件列表
set(COPY_FILES
    "${CMAKE_SOURCE_DIR}/config.ini"
    "${CMAKE_SOURCE_DIR}/greatsky.db"
    "${CMAKE_SOURCE_DIR}/tree_data.json"
)

message(WARNING "runtime path: ${CMAKE_BINARY_DIR}")

# 复制文件到运行目录
foreach(file ${COPY_FILES})
    if(EXISTS "${file}")
        # 获取文件名（不含路径）
        get_filename_component(filename ${file} NAME)
        # 添加复制命令
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${file}"
                "${CMAKE_BINARY_DIR}/${filename}"
            COMMENT "Copying ${filename} to output directory"
        )
        # 安装时也复制（make install时）
        install(FILES ${file} DESTINATION .)
    else()
        message(WARNING "File not found: ${file}")
    endif()
endforeach()

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.expertin6)
endif()
set_target_properties(${PROJECT_NAME} PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

if(QT_VERSION_MAJOR EQUAL 6)
    qt_import_qml_plugins(${PROJECT_NAME})
    qt_finalize_executable(${PROJECT_NAME})
endif()
